package com.cheung.leetcode.day;

import  org.junit.jupiter.api.Test;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.day
 * @Author :  cheungming
 * @CreateTime : 2024-07-13 08:53:09
 * @Description : 每日 一题
 * @Version : 1.0
 */
public class Code3011Test {

    public boolean canSortArray(int[] nums) {
        // 按照题意，类似冒泡排序，但是只有相邻两个元素比较一次，如果满足条件，则交换位置，否则不交换位置，最后判断是否排序完成
        // 先写下冒泡排序代码
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = 0; j < nums.length - 1 - i; j++) {
                // 交换条件
                if (nums[j] > nums[j + 1] && Integer.bitCount(nums[j]) == Integer.bitCount(nums[j + 1])) {
                    int temp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = temp;
                }
            }
        }
        // 判断是否排序完成
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] > nums[i + 1]) {
                return false;
            }
        }


        return true;
    }


    public int getOne(int num) {
        int count = 0;
        while (num > 0) {
            num = num & (num - 1);
            count++;
        }
        return count;
    }


    @Test
    public void test() {
        int max = 1000000;
        long l1 = System.currentTimeMillis();
        for (int i = 0; i < max; i++) {
            getOne(i);
        }
        long l2 = System.currentTimeMillis();
        System.out.println(l2 - l1);

        long l3 = System.currentTimeMillis();
        for (int i = 0; i < max; i++) {
            Integer.bitCount(i);
        }
        long l4 = System.currentTimeMillis();
        System.out.println(l4 - l3);
    }


    @Test
    public void test1() {
        int[] nums = new int[]{1, 2, 3, 4, 5};
        assert canSortArray(nums);
    }


    @Test
    public void test2() {
        int[] nums = new int[]{3, 16, 8, 4, 2};
        assert !canSortArray(nums);
    }

    @Test
    public void test3() {
        int[] nums = new int[]{8, 4, 2, 30, 15};
        assert canSortArray(nums);
    }

    @Test
    public void test4() {
        int[] nums = new int[]{26, 10};
        assert !canSortArray(nums);
    }

    @Test
    public void test5() {
        int[] nums = new int[]{6, 96, 20};
        assert canSortArray(nums);
    }
}
